public class Search {

    /* Constructor Function. The campaign id is captured in this function */
    public Search(ApexPages.StandardController controller){
        cid = System.currentPageReference().getParameters().get('id');
    }
    
    public String cid;

    public String toString(Boolean flag){
        if (flag){
            return 'true';
        } else if (!flag){
            return 'false';
        } else {
            return null;
        }
    }
    
    public Boolean toBoolean(String flag){
        if (flag.equalsIgnoreCase('true')){
            return true;
        } else if (flag.equalsIgnoreCase('false')){
            return false;
        } else {
            return null;
        }
    }
    
    public String query(Map<String, String> queryFields, String customObject){
        String selectQueries = '';
        String whereQueries = '';
        for (String field : queryFields.keySet()){
            selectQueries +=  ', ' + field;
            if (!queryFields.get(field).equals('') && !(customObject.equals('Person__c') && field.equals('Courses__c'))){
                if (field.equals('Is_Tutor__c') || field.equals('Is_Active__c')){
                    whereQueries += field + ' = ' + toBoolean(queryFields.get(field)) + ' AND ';
                } else if (customObject.equals('Availability__c') && field.equals('Name')){
                    whereQueries += field + ' = \'Availability - ' + queryFields.get(field) + '\' AND ';
                } else {
                    whereQueries += field + ' LIKE \'%' + queryFields.get(field) + '%\' AND ';
                }
            }
        }
        String whereStr = '';
        if (whereQueries.length() > 0){
            whereStr = ' WHERE ' + whereQueries.substring(0, whereQueries.length() - 5);
        }
        return 'SELECT Id' + selectQueries + ' FROM ' + customObject + whereStr;
    }
    
    public String personUTORid {get; set;}
    public String personFirstName {get; set;}
    public String personLastName {get; set;}
    public String personEmail {get; set;}
    public String personStudentNumber {get; set;}
    public String personCourses {get; set;}
    public Boolean personIsTutor {get; set;}
    
    public List<Person__c> personList{get; set;}
    public List<Person__c> personFieldList{get; set;}
    public List<Enrollee__c> personCourseList{get; set;}
    public Map<String, String> personFields = new Map<String, String>();
    
    public void inputPersonFields(){
        personFields.put('Name', personUTORid.trim());
        personFields.put('First_Name__c', personFirstName.trim());
        personFields.put('Last_Name__c', personLastName.trim());
        personFields.put('Email__c', personEmail.trim());
        personFields.put('Student_Number__c', personStudentNumber.trim());
        personFields.put('Courses__c', personCourses.trim());
        personFields.put('Is_Tutor__c', toString(personIsTutor));
    }
    
    /* Method to Search the contact database to fetch the query results */
    public void personSearch(){
        inputPersonFields();
        personList = new List<Person__c>();
        personFieldList = new List<Person__c>();
        personCourseList = new List<Enrollee__c>();
        personFieldList = Database.query(query(personFields, 'Person__c'));
        if (personCourses.length() > 0){
            personCourseList = Database.query('SELECT Person__r.id, Course__r.Name FROM Enrollee__c WHERE Course__r.Name LIKE \'%' + personFields.get('Courses__c') + '%\'');
            for (Person__c p : personFieldList){
                Boolean b = false;
                for (Enrollee__c e : personCourseList){
                   if ((p.id)==(e.Person__r.id)){
                        b = true;
                    }
                }
                if (b){
                    personList.add(p);
                }
            }
        }
        else{
            personList = personFieldList;
        }
    }
    /* End of method */


    /* Method for returning the contact search results to the UI */
    public List<Person__c> getPersonResults(){
        return personList;
    }
    /* End of Method */
    
   
    
    
    public String courseCode {get; set; }
    public String courseName {get; set;}
    public String courseDescription {get; set;}
    public String courseDiscipline {get; set;}
    
    public List<Course__c> courseList{get; set;}
    public Map<String, String> courseFields = new Map<String, String>();
    
    public void inputCourseFields(){
        courseFields.put('Name',courseCode.trim());
        courseFields.put('Course_Name__c', courseName.trim());
        courseFields.put('Description__c', '');
        courseFields.put('Discipline__c', courseDiscipline.trim());
    }
    
    /* Method to Search the contact database to fetch the query results */
    public void courseSearch(){
        inputCourseFields();
        courseList = new List<Course__c>();
        courseList = Database.query(query(courseFields, 'Course__c'));
    }
    /* End of method */


    /* Method for returning the contact search results to the UI */
    public List<Course__c> getCourseResults(){
        return courseList;
    }
    /* End of Method */
    
    
    
    
    public String sessionName {get; set; }
    public String sessionPerson {get; set; }
    public String sessionCourse {get; set;}
    public String sessionDay {get; set;}
    public Boolean sessionIsActive {get; set;}
    public Integer sessionCapacity {get; set;}
    
    public List<Session__c> sessionList{get; set;}
    public Map<String, String> sessionFields = new Map<String, String>();
    
    public void inputSessionFields(){
        sessionFields.put('Name', sessionName.trim());
        sessionFields.put('Person__r.Name', sessionPerson.trim());
        sessionFields.put('Course__r.Name', sessionCourse.trim());
        sessionFields.put('Day__c', sessionDay.trim());
        sessionFields.put('Is_Active__c', toString(sessionIsActive));
        sessionFields.put('Capacity__c', '');
    }
    
    /* Method to Search the contact database to fetch the query results */
    public void sessionSearch(){
        inputSessionFields();
        sessionList = new List<Session__c>();
        sessionList = Database.query(query(sessionFields, 'Session__c'));
    }
    /* End of method */


    /* Method for returning the contact search results to the UI */
    public List<Session__c> getSessionResults(){
        return sessionList;
    }
    /* End of Method */
    
    public String availabilityName {get; set;}
    public String availabilityPerson {get; set;}
    public String availabilityDay {get; set;}
    public Boolean availabilityIsActive {get; set;}
    
    public List<Availability__c> availabilityList{get; set;}
    public Map<String, String> availabilityFields = new Map<String, String>();
    
    public void inputAvailabilityFields(){
        availabilityFields.put('Name', availabilityName.trim());
        availabilityFields.put('Person__r.Name', availabilityPerson.trim());
        availabilityFields.put('Day__c', availabilityDay.trim());
        availabilityFields.put('Is_Active__c', toString(availabilityIsActive));
    }
    
    /* Method to Search the contact database to fetch the query results */
    public void availabilitySearch(){
        inputAvailabilityFields();
        availabilityList = new List<Availability__c>();
        System.Debug(query(availabilityFields, 'Availability__c'));
        availabilityList = Database.query(query(availabilityFields, 'Availability__c'));
    }
    /* End of method */


    /* Method for returning the contact search results to the UI */
    public List<Availability__c> getAvailabilityResults(){
        return availabilityList;
    }
    /* End of Method */

}